package cn.mifengkong.appcommon.webview;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.view.View;
import android.webkit.JavascriptInterface;

import com.mifengkong.frtools.util.FRString;
import com.mifengkong.frtools.util.FRToast;
import com.mifengkong.frwidget.dialog.FRDialog;
import com.mifengkong.frwidget.dialog.FRDialogClickListener;
import com.mifengkong.statistic.umengsignin.WeChatSignInManager;

import java.util.ArrayList;
import java.util.List;

import cn.mifengkong.appcommon.util.ActivityStartUtil;
import cn.mifengkong.appcommon.webview.listener.JsCallBackListener;
import cn.mifengkong.appcommon.webview.listener.MyUMShareListener;
import cn.mifengkong.appcommon.webview.listener.SharePromptDialogListener;

import static com.mifengkong.frtools.util.FRToast.showToastSafe;

/**
 * Created by jiangyongxing on 2017/3/20.
 * 描述：
 */

public class DataJavascriptInterface {

    private Activity context;
    private JsCallBackListener mJsCallBackListener;
    private List<SharePromptDialogListener> mMyUMShareListeners = new ArrayList<>();

    public DataJavascriptInterface(Activity context, JsCallBackListener callBackListener) {
        this.context = context;
        this.mJsCallBackListener = callBackListener;
    }

    @JavascriptInterface
    public void close() {
        mJsCallBackListener.closeWebView();
    }


    @JavascriptInterface
    public void shareView(String title, String desc, String url, String thumpImg) {//分享链接
        WeChatSignInManager.with(context)
                .shareTitle(title)
                .shareDesc(desc)
                .shareUrl(url)
                .shareThumpImg(thumpImg)
                .shareView(getMyUMShareListener(context));
    }

    @JavascriptInterface
    public void shareViewWithImage(String imageUrl, String thumpImg) {//分享图片
        WeChatSignInManager.with(context)
                .shareImageUrl(imageUrl)
                .shareThumpImg(thumpImg)
                .shareView(getMyUMShareListener(context));
    }

    @JavascriptInterface
    public void shareViewWithVideo(String title, String desc, String videoUrl, String thumpImg) {
        WeChatSignInManager.with(context)
                .shareTitle(title)
                .shareDesc(desc)
                .shareVideoUrl(videoUrl)
                .shareThumpImg(thumpImg)
                .shareView(getMyUMShareListener(context));
    }

    @JavascriptInterface
    public void shareViewWithMusic(String title, String desc, String musicUrl, String thumpImg) {
        WeChatSignInManager.with(context)
                .shareTitle(title)
                .shareDesc(desc)
                .shareMusicUrl(musicUrl)
                .shareThumpImg(thumpImg)
                .shareView(getMyUMShareListener(context));
    }

    private MyUMShareListener getMyUMShareListener(Context context) {
        MyUMShareListener listener = new MyUMShareListener(context);
        mMyUMShareListeners.add(listener);
        return listener;
    }


    public void setProgressDialogDiamiss() {
        for (SharePromptDialogListener myUMShareListener : mMyUMShareListeners) {
            myUMShareListener.dialogDismiss();
        }
//        mMyUMShareListeners.clear();
    }

    @JavascriptInterface
    public void startActivity(String activityName) {
        startActivity(activityName, "");
    }


    /**
     * @param activityName
     * @param parameter    //parameter 的格式   uid=12&token=36
     */
    @JavascriptInterface
    public void startActivity(String activityName, String parameter) {
        if (mJsCallBackListener == null)
            return;
        if ("LoginActivity".equalsIgnoreCase(activityName)) {
            startLoginActivity();
            return;
        }
        ActivityStartUtil.startActivityByActivityNameAndParameter(context, activityName, parameter, mJsCallBackListener.getPackageName());
    }

    @JavascriptInterface
    public void logout() {
        mJsCallBackListener.logOut();
    }

    @JavascriptInterface
    public void toast(String msg) {
        FRToast.showToastSafe(msg);
    }

    @JavascriptInterface
    public void alert(String title, String content, String positiveTv, String negativeTv) {
        new FRDialog.MDBuilder(context)
                .setTitle(title)
                .setMessage(content)
                .setNegativeContentAndListener(negativeTv, null)
                .setPositiveContentAndListener(positiveTv, new FRDialogClickListener() {
                    @Override
                    public boolean onDialogClick(View view) {
                        return true;
                    }
                }).show();
    }

    @JavascriptInterface
    public void alert(String title, String content, String negativeTv) {
        new FRDialog.MDBuilder(context)
                .setTitle(title)
                .setMessage(content)
                .setNegativeContentAndListener(negativeTv, null)
                .show();
    }

    @JavascriptInterface
    public void customeSerive() {
        mJsCallBackListener.showCustomServiceDialog();
    }

    @JavascriptInterface
    public void startMainActivity() {
        mJsCallBackListener.startMainActivity();
    }

    @JavascriptInterface
    public void startLoginActivity() {
        mJsCallBackListener.startLoginActivity();
    }

    @JavascriptInterface
    public void getCashOrderNumber(String orderNumber) {
        showToastSafe("---");
    }

    @JavascriptInterface
    public void getCashFailureMessage(String msg, String url) {
        if (FRString.isEmpty(msg)) {
            return;
        }
        mJsCallBackListener.getCashFailureMessage(msg, url);
    }

    @JavascriptInterface
    public void openWeixin() {
        ActivityStartUtil.startWeiXin(context);
    }

    @JavascriptInterface
    public void copyText(String text) {
        ClipboardManager mClipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        mClipboardManager.setPrimaryClip(ClipData.newPlainText(text, text));
    }

    public void cardManagerHome() {

    }

    @JavascriptInterface
    public void showNextFragment() {
        mJsCallBackListener.showNextFragment(context);
    }
}